<?php 
require_once 'init.php';
require_once 'ckLogin.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'config.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'dbConn.php';
$db = new dbConn();

$year = isset($_POST['year']) ? htmlspecialchars(trim($_POST['year'])) : date("Y");
$month = isset($_POST['month']) ? htmlspecialchars(trim($_POST['month'])) : date("m");
//$StartTime = "$year-$month-01 00:00:00";
//$days = date("t", strtotime("$year-$month"));
//$EndTime = "$year-$month-$days 23:59:59";
$db->sql="SELECT o.*,a.id,c.`shared`,c.`collected` FROM OrderData o,admin a,((SELECT con.`goodsID`,con.`updater`,con.`publishDate`,con.`shared`,con.`collected` from content con where con.`status`=1) union(SELECT p.`goodsID`,p.updater,p.`publishDate`,p.`shared`,p.`collected` from priceninepointnine p where p.`status`=1)) c where o.GoodsId=c.goodsID AND c.updater=a.userName AND month(c.publishDate)=$month AND month(o.CreateTime)=$month";
$OrderData = $db->doResultList2();
$Selecters = array();
if($OrderData){
	$db->sql="SELECT id,nickName FROM `admin` where `type`=2";
	$users = $db->doResultList2();
	foreach($users as $user){
		$Selecters[$user['id']] = $user;
		$Selecters[$user['id']]["OrderNum"] = 0;
		$Selecters[$user['id']]["LogNum"] = 0;
		$Selecters[$user['id']]["OrderMon"] = 0;
		$Selecters[$user['id']]["FSNum"] = 0;
		$Selecters[$user['id']]["XlJb"] = 0;
		$Selecters[$user['id']]["XlGx"] = 0;
		$Selecters[$user['id']]["YjJb"] = 0;
		$Selecters[$user['id']]["YjGx"] = 0;
		$Selecters[$user['id']]["FxSc"] = 0;
		$Selecters[$user['id']]["xlw"] = 0;
		$Selecters[$user['id']]["yjw"] = 0;
		$Selecters[$user['id']]["sgw"] = 0;
		$Selecters[$user['id']]["All"] = 0;
	}
	$num = count($users);	
	
	$db->sql="SELECT * FROM `PerformFactor` WHERE time='$year-$month'";
	$factor = $db->doResultList2();
	if(!isset($factor[0])){
		$factor[0]["BasePay"] = 1800;
		$factor[0]["ratio"] = 1;
	}
	//全部奖金
	$qbjj = $num*$factor[0]["BasePay"]*$factor[0]["ratio"];
	//销量级别奖金
	$xljb = $qbjj*0.27;
	//销量贡献奖金
	$xlgx = $qbjj*0.18;
	//佣金级别奖金
	$yjjb = $qbjj*0.27;
	//佣金贡献奖金
	$yjgx = $qbjj*0.18;
	//分享收藏奖金
	$fxsc = $qbjj*0.1;
	foreach($OrderData as $val){
		foreach($Selecters as $key => $item){
			if($key == $val['id']){
				switch($val['CurrentPrice']){
					case ($val['CurrentPrice']<20) : $LogNum = $val["OrderNum"]; break;
					case ($val['CurrentPrice']>=20 && $val['CurrentPrice']<40) : $LogNum = $val["OrderNum"] * 1.2; break;
					case ($val['CurrentPrice']>=40 && $val['CurrentPrice']<60) : $LogNum = $val["OrderNum"] * 1.4; break;
					case ($val['CurrentPrice']>=60 && $val['CurrentPrice']<80) : $LogNum = $val["OrderNum"] * 1.6; break;
					case ($val['CurrentPrice']>=80 && $val['CurrentPrice']<100) : $LogNum = $val["OrderNum"] * 1.8; break;
					case ($val['CurrentPrice']>=100) : $LogNum = $val["OrderNum"] * 2; break;
				}
				$Selecters[$key]['OrderNum'] += $val["OrderNum"];
				$Selecters[$key]['LogNum'] += $LogNum;
				$Selecters[$key]["OrderMon"] += round($val["OrderNum"]*$val["CurrentPrice"]*trim($val["OrderRate"], "%")*0.01, 2);
				$Selecters[$key]["FSNum"] += ($val["shared"] + $val["collected"]);
			}
		}
	}
	$AllOrderNum = 0;
	$AllOrderMon = 0;
	$AllFSNum = 0;
	$SumNum = 0;
	for($i=1;$i<=$num;$i++){
		$SumNum += $i;
	}
	foreach($Selecters as $key => $val){
		$AllOrderNum += $val['LogNum']; 
		$AllOrderMon += $val['OrderMon']; 
		$AllFSNum += $val['FSNum'];
		$OrderNum[$key] = $val['LogNum'];
		$OrderMon[$key] = $val['OrderMon']; 
		$FSNum[$key] = $val['FSNum'];
	}
	$YjLastId = 0;
	$XlLastId = 0;
	$XlLtId = 0;
	$YjLtId = 0;
	
	arsort($OrderNum);
	arsort($OrderMon);
	arsort($FSNum);
	GetSort($OrderNum, "Selecters", "NumSort", $num);
	GetSort($OrderMon, "Selecters", "MonSort", $num);
	GetSort($FSNum, "Selecters", "FSSort", $num);

	foreach($Selecters as $key => $val){
		if($key == $XlLastId || $key == $XlLtId){
			$Selecters[$key]["XlJb"] = 0;
			$Selecters[$key]["XlGx"] = 0;
		}
		else{
			$Selecters[$key]["XlJb"] = round(($num-$val["NumSort"]+1)/$SumNum*$xljb, 2);		
			$Selecters[$key]["XlGx"] = round($val['LogNum']/$AllOrderNum*$xlgx, 2);
		}
		if($key == $YjLastId || $key == $YjLtId){
			$Selecters[$key]["YjJb"] = 0;
			$Selecters[$key]["YjGx"] = 0;
		}
		else{
			$Selecters[$key]["YjJb"] = round(($num-$val["MonSort"]+1)/$SumNum*$yjjb, 2);
			$Selecters[$key]["YjGx"] = round($val['OrderMon']/$AllOrderMon*$yjgx, 2);
		}
		$Selecters[$key]["FxSc"] = round($val["FSNum"]/$AllFSNum*$fxsc, 2);
	}
	$TjXlJb = 0;
	$TjXlGx = 0;
	$TjYjJb = 0;
	$TjYjGx = 0;
	$TjFxSc = 0;
	$TjAll = 0;
	
	foreach($Selecters as $key => $val){
		$Selecters[$key]["All"] = $val["XlJb"]+$val["YjJb"]+$val["XlGx"]+$val["YjGx"]+$val["FxSc"];
		if($val["NumSort"] == 1){
			$Selecters[$key]["xlw"] = 100;
			$Selecters[$key]["All"] += $Selecters[$key]["xlw"];
		}
	
		if($val["MonSort"] == 1){
			$Selecters[$key]["yjw"] = 100;
			$Selecters[$key]["All"] += $Selecters[$key]["yjw"];
		}
		
		if($val["NumSort"] == 1 && $val["MonSort"] == 1){
			$Selecters[$key]["sgw"] = 100;
			$Selecters[$key]["All"] += $Selecters[$key]["sgw"];
		}
		
		$TjXlJb += $val["XlJb"];
		$TjXlGx += $val["XlGx"];
		$TjYjJb += $val["YjJb"];
		$TjYjGx += $val["YjGx"];
		$TjFxSc += $val["FxSc"];
		$TjAll += $Selecters[$key]["All"];
	}
	
	$datas = sortArrayDesc($Selecters, "All");
}
function GetSort($odata, $ndata, $param, $num){
	$i = 1;
	global ${$ndata},$YjLastId,$XlLastId,$XlLtId,$YjLtId;
	foreach($odata as $key => $val){
		${$ndata}[$key][$param] = $i;
		if($i==$num && $param=="NumSort"){
			$XlLastId = $key;
		}
		elseif($i==($num-1) && $param=="NumSort"){
			$XlLtId = $key;
		}
		elseif($i==$num && $param=="MonSort"){
			$YjLastId = $key;
		}
		elseif($i==($num-1) && $param=="MonSort"){
			$YjLtId = $key;
		}
		$i++;
	}
}
//按$sortType关键字降序
function sortArrayDesc($preData, $sortType='newPrice'){
	$sortData = array();
	foreach ($preData as $key_i => $value_i){
		$price_i = $value_i[$sortType];
		$min_key = '';
		$sort_total = count($sortData);
		foreach ($sortData as $key_j => $value_j){
			if($price_i>$value_j[$sortType]){
				$min_key = $key_j+1;
				break;
			}
		}
		if(empty($min_key)){
			array_push($sortData, $value_i);
		}else {
			$sortData1 = array_slice($sortData, 0,$min_key-1);
			array_push($sortData1, $value_i);
			if(($min_key-1)<$sort_total){
				$sortData2 = array_slice($sortData, $min_key-1);
				foreach ($sortData2 as $value){
					array_push($sortData1, $value);
				}
			}
			$sortData = $sortData1;
		}
	}
	return $sortData;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simpla Admin</title>
<!--                       CSS                       -->
<!-- Reset Stylesheet -->
<link rel="stylesheet" href="resources/css/reset.css" type="text/css"
	media="screen" />
<!-- Main Stylesheet -->
<link rel="stylesheet" href="resources/css/main.css" type="text/css"
	media="screen" />
<!-- Invalid Stylesheet. This makes stuff look pretty. Remove it if you want the CSS completely valid -->
<link rel="stylesheet" href="resources/css/invalid.css" type="text/css"
	media="screen" />
<!-- Colour Schemes
		Default colour scheme is green. Uncomment prefered stylesheet to use it.
		<link rel="stylesheet" href="resources/css/blue.css" type="text/css" media="screen" />
		<link rel="stylesheet" href="resources/css/red.css" type="text/css" media="screen" />  
		-->
<!-- Internet Explorer Fixes Stylesheet -->
<!--[if lte IE 7]>
			<link rel="stylesheet" href="resources/css/ie.css" type="text/css" media="screen" />
		<![endif]-->
<!--                       Javascripts                       -->
<!-- jQuery -->
<script type="text/javascript" src="resources/scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function($) {
	
})
</script>
<!-- jQuery Configuration -->
<script type="text/javascript" src="resources/scripts/simpla.jquery.configuration.js"></script>
<!-- Facebox jQuery Plugin -->
<script type="text/javascript" src="resources/scripts/facebox.js"></script>
<!-- jQuery WYSIWYG Plugin -->
<script type="text/javascript" src="resources/scripts/jquery.wysiwyg.js"></script>
<!-- jQuery my97datepicker -->
<script type="text/javascript" src="resources/scripts/My97DatePicker/WdatePicker.js"></script>
<!-- jQuery colorbox -->
<link rel="stylesheet" href="resources/css/colorbox.css" />
<script src="resources/scripts/colorbox/jquery.colorbox.js"></script>
</head>
<body>
	<div id="body-wrapper">
		<!-- Wrapper for the radial gradient background -->		
		<div id="main-content">
			<!-- Main Content Section with everything -->
			<!-- Page Head -->
			<h2>欢迎使用<?php echo APPLICATION;?>管理后台</h2>
			<p id="page-intro">接下来你要做什么呢亲？</p>
			<div class="clear"></div>
			<!-- End .clear -->
			<div class="content-box">
				<!-- Start Content Box -->
				<div class="content-box-header">
					<h3>绩效列表</h3>
					<!-- <ul class="content-box-tabs">
						<li><a href="#tab1" class="default-tab">Table</a></li>
						href must be unique and match the id of target div
						<li><a href="#tab2">Forms</a></li>
					</ul> -->
					<div class="clear"></div>
				</div>
				<!-- End .content-box-header -->
				<div class="content-box-content">
					<form method="post" action="">						
						<select name="year">
						<?php for($i=2013;$i<=$year;$i++):?>
							<option value="<?php echo $i;?>" <?php echo ($i==$year)?"selected":"";?>><?php echo $i;?></option>							
						<?php endfor;?>					
						</select>年						
						<select name="month">
						<?php for($i=1;$i<=12;$i++):?>
							<option value="<?php echo ($i<10) ? "0{$i}" : $i;?>" <?php echo ($i==$month)?"selected":"";?>><?php echo ($i<10) ? "0{$i}" : $i;?></option>							
						<?php endfor;?>								
						</select>月
						<button class="button" type="submit" style="margin-left: 10px;">搜索</button>																		
					</form>
					
					<hr />
					<div class="clear"></div>
					<div class="tab-content default-tab" id="tab1">
						<!-- This is the target div. id must match the href of this div's tab -->

						<table>							
							<thead>
								<tr>
									<th style="width:7%;">姓名</th>									
									<th style="width:5%;">销量排名</th>									
									<th style="width:5%;">销量</th>
									<th style="width:7%;">销量级别奖</th>
									<th style="width:7%;">销量贡献奖</th>
									<th style="width:5%;">佣金排名</th>
									<th style="width:6%;">佣金</th>
									<th style="width:7%;">佣金级别奖</th>
									<th style="width:7%;">佣金贡献奖</th>
									<th style="width:6%;">分享收藏排名</th>
									<th style="width:7%;">分享收藏量</th>
									<th style="width:7%;">分享收藏量奖</th>
									<th style="width:6%;">销量王</th>
									<th style="width:6%;">佣金王</th>
									<th style="width:6%;">双冠王</th>
									<th>总奖励金额</th>									
								</tr>
							</thead>
							<tbody>
							<?php if(count($OrderData) > 0):?>	
								<?php foreach($datas as $val):?>
								<tr>
									<td><?php echo $val['nickName'];?></td>
									<td><?php echo $val['NumSort'];?></td>
									<td><?php echo $val['LogNum'];?></td>
									<td><?php echo $val['XlJb'];?></td>
									<td><?php echo $val['XlGx'];?></td>
									<td><?php echo $val['MonSort'];?></td>
									<td><?php echo $val['OrderMon'];?></td>
									<td><?php echo $val['YjJb'];?></td>
									<td><?php echo $val['YjGx'];?></td>
									<td><?php echo $val['FSSort'];?></td>
									<td><?php echo $val['FSNum'];?></td>
									<td><?php echo $val['FxSc'];?></td>
									<td><?php echo ($val['xlw'] !== 0) ? $val['xlw'] : "-";?></td>
									<td><?php echo ($val['yjw'] !== 0) ? $val['yjw'] : "-";?></td>
									<td><?php echo ($val['sgw'] !== 0) ? $val['sgw'] : "-";?></td>
									<td><?php echo $val['All'];?></td>
								</tr>
								<?php endforeach;?>
								<tr>
									<td colspan="13"></td>
								</tr>
								<tr>
									<td>计算情况</td>
									<td></td>
									<td></td>
									<td><?php echo $TjXlJb;?></td>
									<td><?php echo $TjXlGx;?></td>
									<td></td>
									<td></td>
									<td><?php echo $TjYjJb;?></td>
									<td><?php echo $TjYjGx;?></td>
									<td></td>
									<td></td>
									<td><?php echo $TjFxSc;?></td>
									<td></td>
									<td></td>
									<td></td>
									<td><?php echo $TjAll;?></td>
								</tr>
								<tr>
									<td>总额情况</td>
									<td></td>
									<td></td>
									<td><?php echo $xljb;?></td>
									<td><?php echo $xlgx;?></td>
									<td></td>
									<td></td>
									<td><?php echo $yjjb;?></td>
									<td><?php echo $yjgx;?></td>
									<td></td>
									<td></td>
									<td><?php echo $fxsc;?></td>
									<td></td>
									<td></td>
									<td></td>
									<td><?php echo $qbjj;?></td>
								</tr>
							<?php else:?>						
								<tr style="font-size: 50px;text-align: center; font-weight: bold;"><td colspan="16" style="text-align: center;">没有该月绩效数据</td></tr>
							<?php endif;?>
							</tbody>
						</table>
					</div>
					<!-- End #tab1 -->
				</div>
				<!-- End .content-box-content -->
			</div>
			<!-- End .content-box -->
			<div class="clear"></div>
			<div id="footer">
				<small> <!-- Remove this notice or replace it with whatever you want -->
					&#169; Copyright 2009 Your Company | Powered by <a
					href="http://themeforest.net/item/simpla-admin-flexible-user-friendly-admin-skin/46073">Simpla
						Admin</a> | <a href="#">Top</a>
				</small>
			</div>
			<!-- End #footer -->
		</div>
		<!-- End #main-content -->
	</div>
</body>
<!-- Download From www.exet.tk-->
</html>
